﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BU
{
    /**
     * 
     * Manager lié aux Order
     * 
     **/
    public class OrderManager : EntityManager
    {
        /**
         * 
         * Récupère une commande selon un id
         * 
         */
        public Order getOne(int id)
        {
            DAL.OrderDataProvider provider = new DAL.OrderDataProvider();
            DataRow orderRow = provider.findOneCommande(id);

            if (orderRow == null)
                return null;

            // Construction d'une comande
            Order order = new Order();
            order.ID = (int)orderRow["id_order"];
            order.Date = (DateTime)orderRow["created_at"];
            Customer customer = new Customer();
            customer.Name = (string)orderRow["name"];
            customer.Firstname = (string)orderRow["firstname"];
            customer.ID = (int)orderRow["id_customer"];
            order.Customer = customer;

            return order;
        }

        /**
         * 
         * Supprime une commande
         * 
         */
        public bool delete(int id)
        {
            DAL.OrderDataProvider provider = new DAL.OrderDataProvider();
            provider.deleteCommande(id);
            return true;
        }

        /**
         * 
         * Récupère toute les commandes
         * 
         **/
        public List<Order> getAll()
        {
            DAL.OrderDataProvider provider = new DAL.OrderDataProvider();
            DataTable dt = provider.findAllOrder();

            List<Order> orders = new List<Order>();

            foreach (DataRow row in dt.Rows)
            {
                Order order = new Order();
                order.ID = (int)row["id_order"];
                order.Date = (DateTime)row["created_at"];
                Customer customer = new Customer();
                customer.Name = (string)row["name"];
                customer.Firstname = (string)row["firstname"];
                order.Customer = customer;
                orders.Add(order);
            }

            return orders;
        }

        /**
        * 
        * Récupère les produits d'une commande
        * 
        **/
        public List<OrderView> getAllOrderProducts(int id)
        {
            DAL.OrderDataProvider provider = new DAL.OrderDataProvider();
            DataTable dt = provider.findAllproductOrder(id);

            List<OrderView> list_order_views = new List<OrderView>();

            foreach (DataRow row in dt.Rows)
            {
                OrderView ov = new OrderView();

                ov.OrderId = (int)row["order_id_order"];
                ov.OrderDetailQuantity = (int)row["quantity"];

                ov.ProductName = (string)row["name"];
                ov.ProductPrice = (decimal)row["price"];

                //product.Name = (string)row["name"];
                //product.Price = (decimal)row["price"];
                //product.Quantity = (int)row["quantity"];

                list_order_views.Add(ov);
            }

            return list_order_views;
        }
    }
}
